/*

Slave client, will be made cross platform as it comes together
this makes testing easier.

By ELChupathingy

*/

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "packet_manager.h"
#include "heart_beat.h"
#include "authenticate.h"
#include "heart_beat_filter.h"
#include "sink_filter.h"
#include "irc_parser.h"
#include "irc_line.h"

#define BOOST_TEST_MODULE "Slave Tests"
#include <boost/test/included/unit_test.hpp>

/*
BOOST_AUTO_TEST_CASE( Packet_Class_Test )
{
	printf( "\nPacket Class Test\n" );
	heart_beat h;
	h.init( 0, 10, 10, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );

	h.print();

	authenticate a;
	a.init( 0, 10, 10, 113, 13, 1337 );

	a.print();
}

BOOST_AUTO_TEST_CASE( Memory_Pool_Packet_Allocate_Test )
{
	printf( "\nMemory_Pool_Packet_Allocate_Test\n" );

	packet_manager *m = new packet_manager();

	m->print();

	delete m;
}

BOOST_AUTO_TEST_CASE( Packet_Manager_Parse_Test )
{
	printf( "\nPacket_Manager_Parse_Test, buffer allocations done via buffer_pool\n" );


	authenticate a;
	a.init( 0, 10, 10, 1337, 13, 37 );

	packet_manager *m = new packet_manager();

	packet *p = m->parse( (unsigned char *)a.serialize(), 200 );

	BOOST_CHECK_EQUAL( p->getSrc(), 10 );
	BOOST_CHECK_EQUAL( p->getDst(), 10 );

	p->print();

	heart_beat h;
	h.init( 0, 10, 10, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );

	p = m->parse( (unsigned char *)h.serialize(), 200 );

	BOOST_CHECK_EQUAL( p->getSrc(), 10 );
	BOOST_CHECK_EQUAL( p->getDst(), 10 );

	BOOST_CHECK( strcmp( ((heart_beat *)p)->getId(), "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" ) == 0 );

	p->print();

	printf( "\nPacket memory stats\n" );
	p->memory_stats();

	printf( "\nPacket Manager Stats\n" );
	m->print();

	delete m;
}

BOOST_AUTO_TEST_CASE( Packet_Filter_Test )
{
	printf( "\nPacket_Filter_Test\n" );

	heart_beat_filter *hbf = new heart_beat_filter();
	sink_filter *sink = new sink_filter();

	hbf->chain( sink );

	heart_beat h;
	h.init( 0, 10, 10, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );

	packet_manager m;

	hbf->process( m.parse( (unsigned char *)h.serialize(), heart_beat::PACKET_HEARTBEAT_SIZE + packet::PACKET_HEADER_SIZE ) );

	authenticate a;
	a.init( 0, 10, 10, 113, 13, 1337 );

	hbf->process( m.parse( (unsigned char *)a.serialize(), authenticate::PACKET_AUTHENTICATE_SIZE + packet::PACKET_HEADER_SIZE ) );

	delete hbf;
	delete sink;
}


#include "irc_filter_manager.h"
#include "irc_nick_filter.h"
#include "irc_sink_filter.h"

void response( std::string a )
{
	printf( "Woot this worked %s\n", a.c_str() );
}

BOOST_AUTO_TEST_CASE( Irc_Line_Parsing )
{
	irc_parser *parser = new irc_parser();

	irc_line *line = parser->parse( ":elchupathingy!elchupa@0x00-78A0FF74.formlessnetworking.net PRIVMSG mr_pickle :!lol @ storm" );

	line->print();

	irc_filter_manager *manager = new irc_filter_manager();
	irc_nick_filter *nick = new irc_nick_filter( "elchupathingy", boost::lambda::bind( &response, boost::lambda::_1 ) );
	irc_sink_filter *sink = new irc_sink_filter();

	manager->chain( nick )->chain( sink );

	nick->process( line );

	line = parser->parse( ":d4rk!elchupa@0x00-78A0FF74.formlessnetworking.net PRIVMSG mr_pickle :!lol @ storm" );

	line->print();

	nick->process( line );

	delete parser;
	delete manager;
}*/

#include <boost/asio.hpp>

using boost::asio::ip::tcp;

#include "../Common/Client.h"
#include "Irc_Client.h"
#include "../Common/thread_job.h"
#include "../Common/thread_manager.h"

class irc : public thread_job
{
	public:
		irc( boost::asio::io_service &io ) : thread_job( this ), io_s( io ) {};
		void operator()()
		{
			Irc_Client *c = new Irc_Client( io_s, "127.0.0.1", "6667", "#elchupa", "elchupa" );

			c->loop();
		}
		boost::asio::io_service &io_s;
};

BOOST_AUTO_TEST_CASE( Irc_Connection_Event_Firing_Test )
{
	printf( "Irc_Connection_Event_Firing_Test\n" );

	boost::asio::io_service io;

	//tcp::resolver resolver( io );
	//tcp::resolver::query query( "79.99.2.74", "6667" );

	//Client<Irc_Client, 6667, boost::function<Irc_Client *(boost::asio::io_service &)> > client( io, resolver.resolve( query ), 
	//	boost::lambda::bind( boost::lambda::new_ptr<Irc_Client>(), boost::lambda::_1, "#elchupa", "elchupa" ) );

	
	irc a( io );
	thread_manager t( 2 );
	t.addShortJob( a );

	io.run();
}